package com.gc.ykqx.service.util;

import android.text.TextUtils;
import android.util.Log;

import com.gc.ykqx.service.entity.GridItem;
import com.gc.ykqx.service.entity.Past24;
import com.gc.ykqx.service.entity.PreInt;
import com.gc.ykqx.service.entity.Site;
import com.gc.ykqx.service.entity.SiteResponse;
import com.gc.ykqx.service.entity.TemInt;
import com.gc.ykqx.service.entity.WeaResponse;
import com.gc.ykqx.service.entity.WindInt;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wsd on 2017/9/30.
 */

public class DataParse {
    private static final String TAG = "DataParse";
    private static List<String> qyIdList = new ArrayList<>();
    private static List<String> swIdList = new ArrayList<>();
    private static String swSite = "[{\"stationId\":\"RG722\",\"station_name\":\"白雁口\",\"lat\":\"28.82\",\"lon\":\"119.99\"},{\"stationId\":\"RG721\",\"station_name\":\"永祥\",\"lat\":\"28.81\",\"lon\":\"119.96\"},{\"stationId\":\"RG710\",\"station_name\":\"前仓\",\"lat\":\"28.80\",\"lon\":\"120.09\"},{\"stationId\":\"RG402\",\"station_name\":\"永康水文站\",\"lat\":\"28.90\",\"lon\":\"120.01\"},{\"stationId\":\"RG434\",\"station_name\":\"桐塘水库\",\"lat\":\"28.91\",\"lon\":\"120.00\"},{\"stationId\":\"RG435\",\"station_name\":\"水仙洞\",\"lat\":\"28.95\",\"lon\":\"119.94\"},{\"stationId\":\"RG730\",\"station_name\":\"八字墙\",\"lat\":\"29.01\",\"lon\":\"119.92\"},{\"stationId\":\"RG714\",\"station_name\":\"潘川\",\"lat\":\"29.07\",\"lon\":\"120.10\"},{\"stationId\":\"RG717\",\"station_name\":\"柿后\",\"lat\":\"28.96\",\"lon\":\"120.14\"},{\"stationId\":\"RG425\",\"station_name\":\"黄坑口\",\"lat\":\"28.93\",\"lon\":\"120.22\"},{\"stationId\":\"RG639\",\"station_name\":\"四路\",\"lat\":\"29.05\",\"lon\":\"120.22\"},{\"stationId\":\"RG149\",\"station_name\":\"棠溪\",\"lat\":\"28.96\",\"lon\":\"120.30\"},{\"stationId\":\"RG414\",\"station_name\":\"太平水库\",\"lat\":\"29.03\",\"lon\":\"120.18\"},{\"stationId\":\"RG430\",\"station_name\":\"大溪塘\",\"lat\":\"28.86\",\"lon\":\"120.01\"},{\"stationId\":\"RG720\",\"station_name\":\"后渠\",\"lat\":\"29.05\",\"lon\":\"120.06\"},{\"stationId\":\"RG719\",\"station_name\":\"峡源\",\"lat\":\"29.05\",\"lon\":\"120.05\"},{\"stationId\":\"RG426\",\"station_name\":\"三渡溪\",\"lat\":\"29.03\",\"lon\":\"120.07\"},{\"stationId\":\"RG429\",\"station_name\":\"黄坟\",\"lat\":\"28.99\",\"lon\":\"120.04\"},{\"stationId\":\"RG433\",\"station_name\":\"上黄\",\"lat\":\"28.95\",\"lon\":\"120.00\"},{\"stationId\":\"RG448\",\"station_name\":\"龙潭里\",\"lat\":\"29.02\",\"lon\":\"119.96\"},{\"stationId\":\"RG424\",\"station_name\":\"洪塘坑\",\"lat\":\"29.00\",\"lon\":\"120.22\"},{\"stationId\":\"RG428\",\"station_name\":\"泽塘里\",\"lat\":\"29.04\",\"lon\":\"120.10\"},{\"stationId\":\"RG412\",\"station_name\":\"方丘\",\"lat\":\"28.90\",\"lon\":\"120.24\"},{\"stationId\":\"RG411\",\"station_name\":\"珠坑\",\"lat\":\"28.80\",\"lon\":\"120.04\"},{\"stationId\":\"RG410\",\"station_name\":\"三联\",\"lat\":\"28.78\",\"lon\":\"120.04\"},{\"stationId\":\"RG425\",\"station_name\":\"黄坑口水库\",\"lat\":\"28.93\",\"lon\":\"120.22\"},{\"stationId\":\"RG417\",\"station_name\":\"王坑\",\"lat\":\"28.82\",\"lon\":\"120.16\"},{\"stationId\":\"RG413\",\"station_name\":\"杨溪\",\"lat\":\"28.89\",\"lon\":\"120.14\"},{\"stationId\":\"RG711\",\"station_name\":\"大路任\",\"lat\":\"28.86\",\"lon\":\"120.21\"},{\"stationId\":\"RG712\",\"station_name\":\"舟山\",\"lat\":\"28.80\",\"lon\":\"120.17\"},{\"stationId\":\"RG713\",\"station_name\":\"岩后\",\"lat\":\"28.90\",\"lon\":\"120.18\"},{\"stationId\":\"RG408\",\"station_name\":\"大坑\",\"lat\":\"28.78\",\"lon\":\"120.12\"},{\"stationId\":\"RG419\",\"station_name\":\"白云\",\"lat\":\"28.84\",\"lon\":\"120.03\"},{\"stationId\":\"RG834\",\"station_name\":\"西竂\",\"lat\":\"28.03\",\"lon\":\"120.02\"},{\"stationId\":\"RG718\",\"station_name\":\"方山脚\",\"lat\":\"28.98\",\"lon\":\"120.10\"},{\"stationId\":\"RG638\",\"station_name\":\"下赵\",\"lat\":\"29.00\",\"lon\":\"120.30\"},{\"stationId\":\"RG321\",\"station_name\":\"玉川\",\"lat\":\"29.02\",\"lon\":\"120.26\"},{\"stationId\":\"RG416\",\"station_name\":\"石柱\",\"lat\":\"28.85\",\"lon\":\"120.09\"},{\"stationId\":\"RG401\",\"station_name\":\"馆头\",\"lat\":\"28.79\",\"lon\":\"120.09\"},{\"stationId\":\"RG423\",\"station_name\":\"芝英\",\"lat\":\"28.95\",\"lon\":\"120.13\"},{\"stationId\":\"RG422\",\"station_name\":\"古山\",\"lat\":\"28.98\",\"lon\":\"120.16\"},{\"stationId\":\"RG421\",\"station_name\":\"大桥\",\"lat\":\"29.06\",\"lon\":\"120.12\"},{\"stationId\":\"RG427\",\"station_name\":\"邵宅\",\"lat\":\"28.94\",\"lon\":\"120.07\"},{\"stationId\":\"RG431\",\"station_name\":\"永利\",\"lat\":\"28.81\",\"lon\":\"119.96\"},{\"stationId\":\"RG432\",\"station_name\":\"翁家畈\",\"lat\":\"28.84\",\"lon\":\"119.98\"},{\"stationId\":\"RG420\",\"station_name\":\"解放水库\",\"lat\":\"28.87\",\"lon\":\"120.06\"},{\"stationId\":\"RG409\",\"station_name\":\"莲塘山\",\"lat\":\"28.81\",\"lon\":\"120.12\"},{\"stationId\":\"RG418\",\"station_name\":\"源口\",\"lat\":\"28.93\",\"lon\":\"120.17\"}]\n";


    public static String parseSite(ArrayList<Site> sites, List<SiteResponse.DSBean> dsBeans, String siteType) {
        try {
            if (dsBeans != null) {
                if (siteType.equals(Constant.SiteType.SITE_QY)) {//如果是气象站点 加入新的
                    addQySite(sites, siteType);
                } else {
                    Gson gson = new Gson();
                    dsBeans = gson.fromJson(swSite, new TypeToken<List<SiteResponse.DSBean>>() {
                    }.getType());
                }
                for (SiteResponse.DSBean bean : dsBeans) {
                    Site site = new Site();
                    site.setStationId(bean.getStationId());
                    if (siteType.equals(Constant.SiteType.SITE_QY)) {//放入集合用于统计取数据
                        qyIdList.add(bean.getStationId());
                    } else {
                        swIdList.add(bean.getStationId());
                    }
                    site.setStation_name(bean.getStation_name());
                    site.setLat(bean.getLat());
                    site.setLon(bean.getLon());
                    site.setTimeValue(new HashMap<String, String>());
                    changeSite(site, bean);
                    sites.add(site);
                }
            }
            return getSiteIdsBySites(sites);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
    添加气象站点
     */
    private static void addQySite(ArrayList<Site> sites, String siteType) {
        Site site = new Site("58654", "缙云", "28.66", "120.08", new HashMap<String, String>());
        Site site2 = new Site("K9021", "新建镇", "28.75", "120.01", new HashMap<String, String>());
        Site site3 = new Site("K9078", "新碧街道", "28.75", "120.08", new HashMap<String, String>());
        Site site4 = new Site("K9074", "东方镇", "28.75", "120.15", new HashMap<String, String>());
        Site site5 = new Site("K9008", "壶镇镇", "28.81", "120.23", new HashMap<String, String>());
        //   Site site6 = new Site("k9100", "白竹镇", "28.91", "120.28", new HashMap<String, String>());
        Site site6 = new Site("K6072", "郭洞村", "28.81", "119.83", new HashMap<String, String>());
        Site site7 = new Site("58642", "武义", "28.88", "119.8", new HashMap<String, String>());
        Site site8 = new Site("K6104", "桐琴镇", "28.85", "119.95", new HashMap<String, String>());
        Site site9 = new Site("K6183", "巩宅镇", "28.88", "119.9", new HashMap<String, String>());
        Site site10 = new Site("K6044", "茭道", "28.95", "119.91", new HashMap<String, String>());
        Site site11 = new Site("K6184", "金畈村", "28.96", "119.81", new HashMap<String, String>());
        if (sites != null) {
            sites.add(site);
            sites.add(site2);
            sites.add(site3);
            sites.add(site4);
            sites.add(site5);
            sites.add(site6);
            sites.add(site7);
            sites.add(site8);
            sites.add(site9);
            sites.add(site10);
            sites.add(site11);
        }
    }

    /*
    把舟山的经度改为12016
    把永祥的维度改为2881
     */
    private static void changeSite(Site site, SiteResponse.DSBean bean) {
        switch (site.getStationId()) {
            case "K6109":
                site.setLat(bean.getLat());
                site.setLon("120.16");
                break;
            case "K6110":
                site.setLat("28.81");
                site.setLon(bean.getLon());
                break;
        }
    }


    public static void parseWea(List<Site> sites, List<WeaResponse.DSBean> dsBeans, String time, String selectId, String timeType, String special, String diyType) {
        if (special.equals(Constant.Special.DIY)) {
            doCount(sites, dsBeans, diyType);
        } else {
            StringBuffer sb = new StringBuffer();
            try {
                if (dsBeans != null) {
                    for (WeaResponse.DSBean bean : dsBeans) {
                        getField(sites, bean, time, selectId, timeType, sb);
                    }
                    Log.e(TAG, "实况信息->: " + "时间:" + time + "\n" + "  result：" + sb.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (sites != null) {
                sites.size();
            }
        }
    }

    private static void doCount(List<Site> sites, List<WeaResponse.DSBean> dsBeans, String diyType) {
        try {
            for (int i = 0; i < qyIdList.size(); i++) {
                String value = null;
                String staId = qyIdList.get(i);
                double sum = 0.0;
                int count = 0;
                for (int j = 0; j < dsBeans.size(); j++) {
                    WeaResponse.DSBean bean = dsBeans.get(j);
                    if (bean.getStationId().equals(staId) && !TextUtils.isEmpty(bean.getPrecipitation())) {//站号对应 不为空
                        sum += Double.parseDouble(bean.getPrecipitation());
                        count++;
                    }
                }
                if (count != 0) {
                    value = sum + "";
                }
                for (Site site : sites) {
                    if (site.getStationId().equals(staId)) {
                        if (diyType.equals(Constant.Diy.DAY)) {
                            site.setValue(formatValue(value, true));
                        } else {
                            site.setValue(formatValue(value, false));
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "doCount: " + e.getMessage());
        }
    }

    private static void getField(List<Site> sites, Object obj, String time, String selectId, String timeType, StringBuffer sb) throws Exception {
        Field[] fields = obj.getClass().getDeclaredFields(); //获取实体类的所有属性，返回Field数组
        String staId = null;
        String value = null;
        for (int j = 0; j < fields.length; j++) { //遍历所有属性
            Field field = fields[j];
            field.setAccessible(true);
            if (field.getName().equals("stationId")) {
                staId = (String) field.get(obj);
            } else if (!field.getName().equals("serialVersionUID") && field.get(obj) != null) {
                if (timeType.equals(Constant.Time.TIME_DAY) && !DateUtil.isToady(time)) {
                    value = formatValue((String) field.get(obj), true);
                } else {
                    value = formatValue((String) field.get(obj), false);
                }
            }
        }
        sb.append("{站号：" + staId + ", 值：" + value + "} ");
        for (int i = 0; i < sites.size(); i++) {
            Site site = sites.get(i);
            if (site.getStationId().equals(staId)) {
                site.getTimeValue().put(time, value);
                site.setValue(value);
                break;
            }
        }
    }

    /*
 把区域站站号取出来拼接成字符串
*/
    private static String getSiteIdsBySites(List<Site> siteList) {
        String ids = null;
        if (siteList != null && siteList.size() > 0) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < siteList.size(); i++) {
                sb.append(siteList.get(i).getStationId()).append(",");
            }
            String s = sb.toString();
            ids = s.substring(0, s.length() - 1);
        }
        return ids;
    }

    public static void resetSites(ArrayList<Site> sites) {
        try {
            for (Site site : sites) {
                site.setTimeValue(new HashMap<String, String>());
                site.setValue(null);
            }
        } catch (Exception e) {
            Log.e(TAG, "resetSites: " + e.getMessage());
        }
    }

    public static String formatValue(String value, boolean isDivTen) {
        String result = value;
        try {
            if (!TextUtils.isEmpty(value)) {
                DecimalFormat df = new DecimalFormat("0.0");
                double v = Double.parseDouble(value);
                if (isDivTen) {//日表数据需要除以10
                   // v = v / 100;
                    v = v / 10;
                }
                result = df.format(v);
            }
        } catch (Exception e) {
            Log.e(TAG, "formatValue: " + e.getMessage());
        } finally {
            return result;
        }
    }

    public static void clearValue(ArrayList<Site> sites) {
        try {
            for (Site site : sites) {
                site.setValue(null);
            }
        } catch (Exception e) {
            Log.e(TAG, "clearValue: " + e.getMessage());
        }

    }

    /*
    解析过去24小时
     */
    public static Map<String, List> parsePast(List<Past24.DSBean> ds) {
        Map<String, List> listMap = new HashMap<>();
        List<TemInt> temIntList = new ArrayList<>();
        List<PreInt> preIntList = new ArrayList<>();
        List<WindInt> windIntList = new ArrayList<>();
        for (int i = 0; i < ds.size(); i++) {
            Past24.DSBean bean = ds.get(i);
            String stationId = bean.getStationId();
            String time = bean.getObservTimes();
            String temp = bean.getDryBulTemp();
            String pre = bean.getPrecipitation();
            String wind = bean.getWindVelocity();

            TemInt temInt = new TemInt();
            temInt.setStationId(stationId);
            temInt.setObservTimes(time);
            temInt.setDryBulTemp(temp);
            temIntList.add(temInt);

            PreInt preInt = new PreInt();
            preInt.setStationId(stationId);
            preInt.setObservTimes(time);
            preInt.setPrecipitation(pre);
            preIntList.add(preInt);

            WindInt windInt = new WindInt();
            windInt.setStationId(stationId);
            windInt.setObservTimes(time);
            windInt.setWindVelocity(wind);
            windIntList.add(windInt);
        }
        listMap.put(Constant.Type.TEM, temIntList);
        listMap.put(Constant.Type.PRE, preIntList);
        listMap.put(Constant.Type.WIND, windIntList);
        return listMap;
    }

    public static List<GridItem> excludeItem(List<GridItem> items) {
        ArrayList<GridItem> afterItems = new ArrayList<GridItem>();
        String[] array = Constant.ADD_SITEID_ARRAY;
        try {
            for (int i = 0; i < items.size(); i++) {
                GridItem item = items.get(i);
                for (int j = 0; j < array.length; j++) {
                    if (item.getSiteId().equals(array[j])) {
                        afterItems.add(item);
                        items.remove(i);
                        i--;
                        break;
                    }
                }
            }
            items.addAll(afterItems);
            return items;
        } catch (Exception e) {
            Log.e(TAG, "exclude: " + e.getMessage());
            return items;
        }
    }


}